package com.aos.erp.gov.ex.service.impl;


import com.aos.erp.common.bean.IPageBean;
import com.aos.erp.common.bean.PageBean;
import com.aos.erp.common.dao.mapper.Condition;
import com.aos.erp.gov.ex.enums.GovConfigType;
import com.aos.erp.gov.ex.mapper.GovExConfigItemMapper;
import com.aos.erp.gov.ex.model.GovExConfigItem;
import com.aos.erp.gov.ex.service.GovExConfigItemService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.List;


@Service
public class GovExConfigItemServiceImpl extends ServiceImpl<GovExConfigItemMapper, GovExConfigItem> implements GovExConfigItemService {

    @Override
    public GovExConfigItem getById(long id) {
        return baseMapper.selectById(id);
    }

    @Override
    public List<GovExConfigItem> getByConfig(long parentId) {
        Condition condition = new Condition(GovExConfigItem.class);
        condition.eqFiled("parentId", parentId);

        return baseMapper.selectList(condition.getWrapper());
    }

    @Override
    public List<GovExConfigItem> getByConfig(long parentId, GovConfigType parentType) {
        Condition condition = new Condition(GovExConfigItem.class);
        condition.eqFiled("parentId", parentId);
        condition.eqFiled("parentType", parentType);

        return baseMapper.selectList(condition.getWrapper());
    }

    @Override
    public GovExConfigItem addBean(GovExConfigItem bean) {
        baseMapper.insert(bean);
        return bean;
    }

    @Override
    public GovExConfigItem saveConfigItem(GovExConfigItem bean) {
        GovExConfigItem configItem = getByCode(bean.getParentId(), bean.getParentType(), bean.getItemType());
        if (configItem == null) {
            //新增
            bean.setId(0);
            bean.setUpdateTime(Calendar.getInstance().getTime());
            baseMapper.insert(bean);
            return bean;

        } else {
            //修改
            if (!StringUtils.equals(configItem.getItemValue(), bean.getItemValue())) {
                configItem.setItemValue(bean.getItemValue());
                configItem.setUpdateTime(Calendar.getInstance().getTime());
                baseMapper.updateById(configItem);
            }
            return configItem;
        }
    }

    private GovExConfigItem getByCode(long parentId, GovConfigType parentType, int itemType) {
        Condition condition = new Condition(GovExConfigItem.class);
        condition.eqFiled("parentId", parentId);
        condition.eqFiled("parentType", parentType);
        condition.eqFiled("itemType", itemType);
        return baseMapper.selectOneBean(condition.getWrapper());

    }

    @Override
    public PageBean<GovExConfigItem> queryByPage(long pageIndex, long pageSize) {

        IPageBean<GovExConfigItem> pageBean = baseMapper.createBizPage(pageIndex, pageSize);
        Condition condition = new Condition(GovExConfigItem.class);
        condition.orderByDesc("id");

        return (PageBean<GovExConfigItem>) baseMapper.selectPage(pageBean, condition.getWrapper());
    }
}